<?php
/*class ShopProduct{
    public $title = 'default product';
    public $producerMainName ;
    public $producerFirstName;
}
$product1 = new ShopProduct();
$product1->producerSecoundName = '第三个名字';*/
/*var_dump($product1->title);
var_dump($product1->producerMainName);
var_dump($porduct1->producerSecoundName);*/

/*class ShopProduct{
    public $title = 'default product';
    public $producerMainName ;
    public $producerFirstName;
    public $numPages;
    public $playLength;
    public $price;

    public function __construct($title,$mainName,$firstName,$numPage,$playLength,$price)
    {
        $this->title = $title;
        $this->producerFirstName = $firstName;
        $this->producerMainName = $mainName;
        $this->numPages = $numPage;
        $this->playLength = $playLength;
        $this->price = $price;
    }

    public function getProducer(){
        return $this->producerFirstName.$this->producerMainName;
    }

    public function getSummaryLine(){
        $base = $this->title.'('.$this->producerFirstName.$this->producerMainName.')';
        return $base;
    }

}

class CdProduct extends ShopProduct{
    function getPlayLength(){
        return $this->playLength;
    }

    public function getSummaryLine(){
        $base = $this->title.'('.$this->producerFirstName.$this->producerMainName.') : playing time'.$this->playLength;
        return $base;
    }
}

class BookProduct extends ShopProduct{
    function getNumOfPages(){
        return $this->numPages;
    }

    public function getSummaryLine(){
        $base = $this->title.'('.$this->producerFirstName.$this->producerMainName.') : page count'.$this->numPages;
        return $base;
    }
}*/

//修改例子，让每个类只处理自己的数据

class ShopProduct{
    public $title = 'default product';
    public $producerMainName ;
    public $producerFirstName;
    private $price;
    public $discount = 0;

    public function __construct($title,$mainName,$firstName,$price)
    {
        $this->title = $title;
        $this->producerFirstName = $firstName;
        $this->producerMainName = $mainName;
        $this->price = $price;
    }

    public function getProducer(){
        return $this->producerFirstName.$this->producerMainName;
    }

    public function getSummaryLine(){
        $base = $this->title.'('.$this->producerFirstName.$this->producerMainName.')';
        return $base;
    }

    public function setDiscount($num){
        $this->discount = $num;
    }

    public function getPrice(){
        return $this->price - $this->discount;
    }

}

class CdProduct extends ShopProduct{
    public $playLength;
    public function __construct($title, $mainName, $firstName, $price,$playLength)
    {
        parent::__construct($title, $mainName, $firstName, $price);
        $this->playLength = $playLength;
    }

    function getPlayLength(){
        return $this->playLength;
    }

    public function getSummaryLine(){

        /*$base = $this->title.'('.$this->producerFirstName.$this->producerMainName.') : playing time'.$this->playLength;*/
        //调用父类的方法，不用重复写同样的代码
        $base = parent::getSummaryLine();
        $base .= ': playing time'.$this->playLength;
        return $base;
    }
}

class BookProduct extends ShopProduct{
    public $numPages;
    public function __construct($title, $mainName, $firstName, $price,$numPages)
    {
        parent::__construct($title, $mainName, $firstName, $price);
        $this->numPages = $numPages;
    }

    function getNumOfPages(){
        return $this->numPages;
    }

    public function getSummaryLine(){
        /*$base = $this->title.'('.$this->producerFirstName.$this->producerMainName.') : page count'.$this->numPages;*/
        //调用父类的方法，不用重复写同样的代码
        $base = parent::getSummaryLine();
        $base .= ':page count'.$this->numPages;
        return $base;
    }
}

class ShopProductWriter{
    private $shopProduct = array();
    public $aa;

    public function addProudct(ShopProduct $shopProduct){
        $this->shopProduct[] = $shopProduct;
    }

    public function write(){
        $str = '';
        foreach($this->shopProduct as $shopProduct){
            $str .= $shopProduct->title.':';
            $str .= $shopProduct->getProducer;
            $str .= '('.$shopProduct->getPrice.')';
        }
    }
}

/*var_dump(get_class_methods('ShopProductWriter'));*/
/*var_dump(get_class_vars('ShopProductWriter'));*/
/*var_dump(get_parent_class('BookProduct'));*/
var_dump(is_subclass_of('BookProduct','ShopProduc'));//检查一个类是否是另一个类的派生类
$returnVal = call_user_func('myFunction');//调用一个方法
$returnVal = call_user_func(array(new ShopProduct(),'getPrice'));//调用一个类方法
$returnVal = call_user_func(array(new ShopProduct(),'getPrice'),20);//带参数